//
//  MServerConfigCache.m
//  MClient
//
//  Created by  史正烨 on 09-10-3.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//


#import "MServerConfigCache.h"


@implementation MServerConfigCache
//@synthesize menus;


static MServerConfigCache *mServerConfigCacheInstance = nil;
static NSString *menuFile = nil;
static NSMutableDictionary *menus = nil;
static NSString *listLayoutFile = nil;
static NSMutableDictionary *listLayouts = nil;
static NSString *formLayoutFile = nil;
static NSMutableDictionary *formLayouts = nil;
static NSString *lookupConfigFile = nil;
static NSMutableDictionary *lookupConfigs = nil;
static NSString *mclientConfigFile = nil;
static NSMutableDictionary *mclientConfigs = nil;
static NSString *reportQueryLayoutFile = nil;
static NSMutableDictionary *reportQueryLayouts = nil;

#pragma mark ---- singleton object methods ----

// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info

+ (MServerConfigCache *)sharedInstance {
	@synchronized(self) {
		if (mServerConfigCacheInstance == nil) {
			mServerConfigCacheInstance = [[self alloc] init]; // assignment not done here
			//			[mServerConfigCacheInstance retain];
			NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
			NSString *documentDirectory = [paths objectAtIndex:0];
			menuFile = [documentDirectory stringByAppendingPathComponent:@"Menu.plist"];
			[menuFile retain];
			listLayoutFile = [documentDirectory stringByAppendingPathComponent:@"ListLayout.plist"];
			[listLayoutFile retain];
			formLayoutFile = [documentDirectory stringByAppendingPathComponent:@"FormLayout.plist"];
			[formLayoutFile retain];
			lookupConfigFile = [documentDirectory stringByAppendingPathComponent:@"LookupConfig.plist"];
			[lookupConfigFile retain];
			mclientConfigFile = [documentDirectory stringByAppendingPathComponent:@"Config.plist"];
			[mclientConfigFile retain];
			reportQueryLayoutFile = [documentDirectory stringByAppendingPathComponent:@"ReportQueryLayout.plist"];
			[reportQueryLayoutFile retain];
		}
	}
	return mServerConfigCacheInstance;
}

+ (id)allocWithZone:(NSZone *)zone {
	@synchronized(self) {
		if (mServerConfigCacheInstance == nil) {
			mServerConfigCacheInstance = [super allocWithZone:zone];
			return mServerConfigCacheInstance;  // assignment and return on first allocation
		}
	}
	return nil; // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
	return self;
}

- (id)retain {
	return self;
}

- (unsigned)retainCount {
	return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release {
	//do nothing
}

- (id)autorelease {
	return self;
}

#pragma mark ---- 缓存文件存取 ----
- (void)loadCacheFromStore:(NSString *)timeStamp server:aServer party:aParty user:aUser {
	[mclientConfigs release]; mclientConfigs = nil;
	mclientConfigs = [[NSMutableDictionary alloc]initWithContentsOfFile:mclientConfigFile];
	MCLog(@"%@,%@",timeStamp,[mclientConfigs objectForKey:@"timeStamp"]);
	
	[menus release]; menus = nil;
	[listLayouts release]; listLayouts = nil;
	[formLayouts release]; formLayouts = nil;
	[lookupConfigs release]; lookupConfigs = nil;
	[reportQueryLayouts release]; reportQueryLayouts = nil;
	
	if ([timeStamp isEqualToString:[mclientConfigs objectForKey:@"timeStamp"]]
		&& [aServer isEqualToString:[mclientConfigs objectForKey:@"server"]]
		&& [aParty isEqualToString:[mclientConfigs objectForKey:@"party"]]
		&& [aUser isEqualToString:[mclientConfigs objectForKey:@"user"]]) {
		MCLog(@"same timeStamp");
		menus = [[NSMutableDictionary alloc]initWithContentsOfFile:menuFile];
		listLayouts = [[NSMutableDictionary alloc]initWithContentsOfFile:listLayoutFile];
		formLayouts = [[NSMutableDictionary alloc]initWithContentsOfFile:formLayoutFile];
		lookupConfigs = [[NSMutableDictionary alloc]initWithContentsOfFile:lookupConfigFile];
		reportQueryLayouts = [[NSMutableDictionary alloc]initWithContentsOfFile:reportQueryLayoutFile];
	}
	else {
		MCLog(@"different timeStamp");
		[mclientConfigs setObject:timeStamp forKey:@"timeStamp"];
		[mclientConfigs setObject:aServer forKey:@"server"];
		[mclientConfigs setObject:aParty forKey:@"party"];
		[mclientConfigs setObject:aUser forKey:@"user"];
		menus = [[NSMutableDictionary alloc]init];
		listLayouts = [[NSMutableDictionary alloc] init];
		formLayouts = [[NSMutableDictionary alloc] init];
		lookupConfigs = [[NSMutableDictionary alloc] init];
		reportQueryLayouts = [[NSMutableDictionary alloc] init];
	}
}

- (void)saveCacheToStore {
	//	MCLog(@"save cache to store:%@",menus);
	[mclientConfigs writeToFile:mclientConfigFile atomically:YES];
	[mclientConfigs release]; mclientConfigs = nil;
	
	[menus writeToFile:menuFile atomically:YES];
	[menus release]; menus = nil;
	
	[listLayouts writeToFile:listLayoutFile atomically:YES];
	[listLayouts release]; listLayouts = nil;
	
	[formLayouts writeToFile:formLayoutFile atomically:YES];
	[formLayouts release]; formLayouts = nil;
	
	[lookupConfigs writeToFile:lookupConfigFile atomically:YES];
	[lookupConfigs release]; lookupConfigs = nil;
	
	[reportQueryLayouts writeToFile:reportQueryLayoutFile atomically:YES];
	[reportQueryLayouts release]; reportQueryLayouts = nil;
}

- (void)addMenu:(NSObject *)subMenus forKey:(NSString *)key {
	if (nil == subMenus || nil == key) {
		return;
	}
	if (menus == nil) {
		menus = [[NSMutableDictionary alloc] init];
	}
	[menus setObject:subMenus forKey:key];
}

- (void)addListLayout:(NSObject *)listLayout forKey:(NSString *)key {
	if (nil == listLayout || nil == key) {
		return;
	}
	if (listLayouts == nil) {
		listLayouts = [[NSMutableDictionary alloc] init];
	}
	[listLayouts setObject:listLayout forKey:key];
}

- (void)addFormLayout:(NSObject *)formLayout forKey:(NSString *)key {
	if (nil == formLayout || nil == key) {
		return;
	}
	if (formLayouts == nil) {
		formLayouts = [[NSMutableDictionary alloc] init];
	}
	[formLayouts setObject:formLayout forKey:key];
}

- (void)addLookupConfig:(NSObject *)lookupConfig forKey:(NSString *)key {
	if (nil == lookupConfig || nil == key) {
		return;
	}
	if (lookupConfigs == nil) {
		lookupConfigs = [[NSMutableDictionary alloc] init];
	}
	[lookupConfigs setObject:lookupConfig forKey:key];
}

- (void)addReportQueryLayout:(NSObject *)reportQueryLayout forKey:(NSString *)key {
	if (nil == reportQueryLayout || nil == key) {
		return;
	}
	if (reportQueryLayouts == nil) {
		reportQueryLayouts = [[NSMutableDictionary alloc] init];
	}
	[reportQueryLayouts setObject:reportQueryLayout forKey:key];
	
}	
#pragma mark ---- 缓存文件存取DAO实现 ----

- (id)getApps {
	return [menus objectForKey:@"Apps:"];
}

- (id)getMenus:(NSString *)parentMenuId {
	return [menus objectForKey:parentMenuId];
}

- (id)getMenuOfApp:(NSString *)appId {
	return [menus objectForKey:[NSString stringWithFormat:@"MenuOfApp:%@",appId]];
}

- (id)getSubMenu:(NSString *)menuId {
	return [menus objectForKey:[NSString stringWithFormat:@"SubMenu:%@",menuId]];
}

- (id)getListConfig:(NSString *)dataObjectName layoutName:(NSString *)layoutName {
	return [listLayouts objectForKey:[NSString stringWithFormat:@"%@:%@:%@", @"ListConfig:", dataObjectName, layoutName]];
}

- (id)getFormConfig:(NSString *)dataObjectName layoutName:(NSString *)layoutName editable:(NSString *)editable {
	return [formLayouts objectForKey:[NSString stringWithFormat:@"%@:%@:%@:%@", @"FormConfig:", dataObjectName, layoutName, editable]];
}

- (id)getReportConfig:(NSString *)reportName {
	return [reportQueryLayouts objectForKey:[NSString stringWithFormat:@"%@:%@", @"ReportConfig:",reportName]];
}


@end
